package com.kx.platform.caffeine;

import com.kx.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

@Service("caffeineCacheService")
public class CaffeineCacheServiceImpl {
    @Autowired
    CacheManager caffeineCacheManager;

    private final static String DEFAULT_CACHE = "default";

/*    public <T> T getValue(Object key) {
        if(key == null) return null;

        Cache cache = caffeineCacheManager.getCache(DEFAULT_CACHE);
        if(cache != null) {
            Cache.ValueWrapper wrapper = cache.get(key);
            if (wrapper != null)
                return (T) wrapper.get();
        }

        return null;
    }*/

    public <T> T getValue(String cacheName, Object key) {
        if(StringUtils.isEmpty(cacheName) || key == null) return null;

        Cache cache = caffeineCacheManager.getCache(cacheName);
        if(cache != null) {
            Cache.ValueWrapper wrapper = cache.get(key);
            if (wrapper != null)
                return (T) wrapper.get();
        }

        return null;
    }

/*    public void putValue(Object key, Object value) {
        if(key == null || value == null) return;

        Cache cache = caffeineCacheManager.getCache(DEFAULT_CACHE);
        if(cache != null) {
            cache.put(key, value);
        }
    }*/

    public void putValue(String cacheName, Object key, Object value) {
        if(StringUtils.isEmpty(cacheName) || key == null || value == null) return;

        Cache cache = caffeineCacheManager.getCache(cacheName);
        if(cache != null) {
            cache.put(key, value);
        }
    }

    /**
     * 删除缓存库中指定key
     * @param cacheName
     * @param key
     */
    public void delValue(String cacheName, Object key) {
        if(StringUtils.isEmpty(cacheName) || key == null) return;

        Cache cache = caffeineCacheManager.getCache(cacheName);

        if(cache != null) {
            cache.evict(key);
        }
    }

    /**
     * 清空缓存库
     * @param cacheName
     */
    public void clear(String cacheName) {
        if(StringUtils.isEmpty(cacheName)) return;

        Cache cache = caffeineCacheManager.getCache(cacheName);

        if(cache != null) {
            cache.clear();
        }
    }

    /**
     * 获取caffeine cache实体，拿到cache map
     * @param cacheName
     * @return
     */
    public ArrayList getNativeCache(String cacheName){
        if(StringUtils.isEmpty(cacheName)) return null;

        Cache cache = caffeineCacheManager.getCache(cacheName);
        if(cache != null) {
            com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCache = (com.github.benmanes.caffeine.cache.Cache<Object, Object>) cache.getNativeCache();
            ConcurrentMap<Object, Object> nativeMap = nativeCache.asMap();
            ArrayList keyList = new ArrayList(nativeMap.keySet());
            if(keyList != null){
                return keyList;
            }
        }
        return new ArrayList();
    }
}